package com.dingding.transfer.stamper.handler;

import com.dingding.transfer.stamper.constant.RabbitConstants;
import com.dingding.transfer.stamper.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/4/8 14:01
 **/
@Slf4j
public class DelayMessageSenderFactory {
     public static class DelayMessageSender{
        private static final DelayMessageSender instance = new DelayMessageSender();

        private DelayMessageSender(){}

        public void sendDelayMessage(String info, Integer delay) {
            log.info("发送延时任务消息：{},延迟时间：{}", info, delay);
            RabbitTemplate rabbitTemplate = SpringContextUtil.getBean(RabbitTemplate.class);
            rabbitTemplate.convertAndSend(RabbitConstants.Exchange.DELAY_EXCHANGE_NAME, RabbitConstants.RoutingKey.DELAY_ROUTING_KEY,
                    info, message -> {
                        message.getMessageProperties().setDelay(delay);
                        return message;
                    });
        }
    }


    public static DelayMessageSender getInstance(){
        return DelayMessageSender.instance;
    }
}
